﻿myApp.controller("auctionController", function ($scope, $http, $modal, modalService, $sce, ngTableParams, $filter,$rootScope) {
    document.title = "Penang Realty AUCTION 世界地產 拍卖";
    function pageInit() {
        $scope.loader = { loading: true };
        var url = 'api/Auction/GetAuction';
        //modified by yuggo hor 
        $scope.data.searchData = [];
        $http.get('api/Property/GetState')
       .success(function (data) {
           $scope.states = data;
           $scope.search();
       })
        //end of modification 
        $http.get(url).success(function (data) {            
            $scope.data.Auctions = data;
            //console.log($scope.data.Auctions);

            $scope.tableParams = new ngTableParams({
                page: 1,            // show first page
                count: 25,           // count per page
                filter: {
                    Type: ''       // initial filter
                },
                sorting: {
                       Price: 'asc'     // initial sorting
                }

            }, {
                total: $scope.data.Auctions.length, // length of data                
                getData: function ($defer, params) {

                    var orderedAuctions = params.filter() ? $filter('filter')($scope.data.Auctions, params.filter()) : $scope.data.Auctions;
                    // use build-in angular filter
                    orderedAuctions = params.sorting() ?  $filter('orderBy')(orderedAuctions, params.orderBy()) : orderedAuctions;
                    params.total(orderedAuctions.length); // set total for recalc pagination
                    $defer.resolve(orderedAuctions.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                    $scope.loader = { loading: false };

                }
            })
        })
        //modified by yuggohor
        
        $scope.changeState = function () {
            if ($scope.property.StateId) {
                $http.get('api/Property/GetLocation/' + $scope.property.StateId)
               .success(function (data) {

                   $scope.locations = data;
               }
               )
            }
        }
        $scope.search = function () {
            $scope.loader.loading = true;
            validateSearch();

            console.log("Scope=");
            console.log($scope);
            console.log($scope.tableParams);
            if ($scope.tableParams !== undefined) $scope.tableParams.reload();

            if ($scope.property.transact == "S") url = 'api/PropertySearch/GetSaleProperty';
            else url = 'api/PropertySearch/GetRentProperty';
            //console.log("$scope.property : ");
            //location.hash = "#notification";
            //var element_to_scroll_to = document.getElementById('notification');
            //element_to_scroll_to.scrollIntoView();

            $http.post(url, $scope.property)
                 .success(function (result) {
                     console.log(result);
                     $scope.loader.loading = false;
                     $scope.data.searchData = result;
                     window.prerenderReady = true;

                 })
                 .error(function (result) {
                     console.log("Error");
                     console.log(result);
                     $scope.loader.loading = false;
                     window.prerenderReady = true;

                 }
             )
        }
        $scope.$watch('data.searchData', function (newValue, oldValue) {
            if ($scope.tableParams) $scope.tableParams.reload();
        });

        $scope.go = function (key, value) {
            console.log($scope.property.transact);
            value['transact'] = $scope.property.transact;
            var jsonObj = JSON.stringify(value);
            sessionStorage.setItem(key, jsonObj);
        }

        $scope.clearEntry = function () {
            $scope.property.Type = "";
            $scope.property.StateId = "";
            $scope.property.transact = "";
            $scope.property.location = "";
            $scope.property.project = "";
            $scope.property.priceMin = 0;
            $scope.property.priceMax = 99999999;
            $scope.data.searchData = [];
        }

        validateSearch = function () {

            if (typeof ($scope.property["priceMax"]) !== "undefined")
                if ($scope.property.priceMax == "null")
                    $scope.property.priceMax = null;

            $scope.property.saleType = $scope.property.transact + $scope.property.Type;
        }
        //end of modification 
    }

    $scope.$emit('newPageLoaded', { 'title': 'Properties Auction in Penang', 'description': 'Buy below market price! House/Factory/Land/Condo/Shop' });

    pageInit();


})